
package com.provafinal.service;

import java.io.Serializable;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.transaction.Status;
import javax.transaction.UserTransaction;

@RequestScoped
public abstract class Service implements Serializable {

    @Resource
    private UserTransaction ut;

    protected void commitTransaction() {
        try {
            if (ut.getStatus() == Status.STATUS_ACTIVE) {
                ut.commit();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    protected void beginTransaction() {
        try {
            if (ut.getStatus() != Status.STATUS_ACTIVE) {
                ut.begin();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    protected void rollBackTransaction() {
        try {
            if (ut.getStatus() == Status.STATUS_ACTIVE) {
                ut.rollback();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}
